var ture = 0;
var kdnum = 0;
window.onkeydown = function() {
	//键盘输入是ascii码值！！！！！！！！！
	kdnum += 1;
	var top1 = document.getElementById("top1");
	for(var i = 0; i < top1.children.length; i++) {
		var code = top1.children[i].getAttribute("code")
		if(event.keyCode == code) {
			top1.removeChild(top1.children[i]);
			ture += 1;
			break;
		}
	}
}
var time = 0;

function jifenban() {
	time += 1
	var top1 = document.getElementById("top1");
	var jifenban = document.getElementById("jifenban");
	if(time <= 9) {
		jifenban.innerHTML = "时间：00:00:0" + time
	} else if(time <= 60 && time > 9) {
		jifenban.innerHTML = "时间：00:00:" + time
	} else if(time > 60) {
		jifenban.innerHTML = "时间：00:0" + Math.floor(time / 60) + ":" + time % 60
	}
	if(ture == 0 || kdnum == 0) {
		var turelv = parseInt((ture / kdnum) * 100);
		jifenban.innerHTML += "<br>" + "正确率：" + 0 + "%"
	} else {
		var turelv = parseInt((ture / kdnum) * 100);
		jifenban.innerHTML += "<br>" + "正确率：" + turelv + "%";
	}
	jifenban.innerHTML += "<br>" + "速度：" + 1 / (1 / 60) + "字/分";
	var lzdiv = document.getElementById("lzpg");
	lzdiv.innerHTML = null;
	var left = 630;
	var top = 500;
	if(ture >= 10) {
		//只要成立就放一个，仅一个，目的是给行内样式
		for(var i = 0; i < 1; i++) {
			var lzpg = document.createElement("img");
			lzpg.className = "lzpg";
			lzpg.style.left = left + "px";
			lzpg.style.top = top + "px";
			lzpg.src = "img/苹果2.png";
			lzdiv.appendChild(lzpg);
		}
	}
	if(ture >= 20) {
		for(var i = 1; i < 2; i++) {
			left += 45;
			var lzpg = document.createElement("img");
			lzpg.className = "lzpg";
			lzpg.style.left = left + "px";
			lzpg.style.top = top + "px";
			lzpg.src = "img/苹果2.png";
			lzdiv.appendChild(lzpg);
		}
	}
	if(ture >= 30) {
		for(var i = 2; i < 3; i++) {
			left += 45;
			var lzpg = document.createElement("img");
			lzpg.className = "lzpg";
			lzpg.style.left = left + "px";
			lzpg.style.top = top + "px";
			lzpg.src = "img/苹果2.png";
			lzdiv.appendChild(lzpg);
		}
	}
	if(ture >= 40) {
		for(var i = 3; i < 4; i++) {
			left -= 20;
			top -= 30;
			var lzpg = document.createElement("img");
			lzpg.className = "lzpg";
			lzpg.style.left = left + "px";
			lzpg.style.top = top + "px";
			lzpg.src = "img/苹果2.png";
			lzdiv.appendChild(lzpg);
		}
	}
	if(ture >= 50) {
		for(var i = 4; i < 5; i++) {
			left -= 40;
			var lzpg = document.createElement("img");
			lzpg.className = "lzpg";
			lzpg.style.left = left + "px";
			lzpg.style.top = top + "px";
			lzpg.src = "img/苹果2.png";
			lzdiv.appendChild(lzpg);
		}
	}
	if(ture >= 60) {
		for(var i = 5; i < 6; i++) {
			left += 20;
			top -= 40;
			var lzpg = document.createElement("img");
			lzpg.className = "lzpg";
			lzpg.style.left = left + "px";
			lzpg.style.top = top + "px";
			lzpg.src = "img/苹果2.png";
			lzdiv.appendChild(lzpg);
		}
	}
	if(ture >= 70) {
		var guoguan = document.getElementById("guoguan");
		if(guoguan.children.length > 0) {
			guoguan.removeChild(guoguan.children[0]);
		}
		var tsdiv = document.createElement("div");
		tsdiv.setAttribute("class", "success");
		tsdiv.innerHTML = "恭喜你过关了!";
		var color = "rgb(" + Math.floor(Math.random() * 256) + "," + Math.floor(Math.random() * 256) + "," + Math.floor(Math.random() * 256) + ")";
		tsdiv.style.backgroundColor = color;
		guoguan.appendChild(tsdiv);
	}
}
var aa = 0;
var mm = 0;
var ff = 0;

function start() {
	if(ture >= 70) {
		jieshu();
	}
	if(aa == 0 && mm == 0 && ff == 0) {
		aa = setInterval("add()", 1000);
		mm = setInterval("move()", 200);
		ff = setInterval("jifenban()", 1000);
	}
}

function zanting() {
	clearTimeout(aa);
	clearTimeout(mm);
	clearTimeout(ff);
	aa = 0;
	mm = 0;
	ff = 0;
}

function jieshu() {
	if(ture >= 70) {
		var guoguan = document.getElementById("guoguan");
		guoguan.removeChild(guoguan.children[0]);
	}
	var jifenban = document.getElementById("jifenban");
	clearTimeout(aa);
	clearTimeout(mm);
	clearTimeout(ff);
	var top1 = document.getElementById("top1");
	top1.innerHTML = null;
	time = 0;
	ture = 0;
	kdnum = 0;
	aa = 0;
	mm = 0;
	ff = 0;
	jifenban.innerHTML = "时间：00：00：00<br />正确率：0%<br />速度：0字/分<br />";
}

function move() {
	var top1 = document.getElementById("top1");
	for(var i = 0; i < top1.children.length; i++) {
		var top = parseInt(top1.children[i].style.top);
		top += 25;
		top1.children[i].style.top = top + "px";
		if(top > 380) {
			top1.children[i].innerHTML = "";
			top1.children[i].className = "bad";
		}
		if(top > 400) {
			top1.removeChild(top1.children[i]);
		}
	}
}

function add() {
	var top1 = document.getElementById("top1");
	var apple = document.createElement("div");
	var num = parseInt(Math.random() * 26) + 65;
	apple.innerHTML = "&#" + num + ";";
	apple.setAttribute("code", num)
	var left = parseInt(Math.random() * 700) + 10;
	apple.style.left = left + "px";
	apple.style.top = "-50px";
	top1.appendChild(apple);
}